package xycdao

import (
	"Cgo/backend/models"
	"Cgo/global"
)

type xycUserDao struct{}

var XycUserDao = xycUserDao{}

// 添加用户
func (xycUserDao) AddUser(user models.XycUser) error {
	if r := global.DB.Create(&user); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}

// 删除用户
func (xycUserDao) DeleteUser(user models.XycUser) error {
	if r := global.DB.Delete(&user); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}

// 更新用户
func (xycUserDao) UpdateUser(user models.XycUser) error {
	if r := global.DB.Save(&user); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}

// 根据id查询用户，不传id则查询所有用户
func (xycUserDao) FindUser(user models.XycUser) ([]models.XycUser, error) {
	var users []models.XycUser
	if r := global.DB.Where(&user).Find(&users); r.Error != nil {
		return users, r.Error
	} else {
		return users, nil
	}
}
